<?php
/**
 * Created by PhpStorm.
 * User: wchuang
 * Date: 2016/8/27
 * Time: 15:05
 */
namespace backend\controllers;

use common\models\LydbUser;
use common\models\LydbUserQuery;
use common\services\BuyService;
use common\services\UserService;
use yii;
use yii\web\Controller;
use yii\filters\VerbFilter;

/**
 * LydbBuyController implements the CRUD actions for LydbUser model.
 */
class LydbBuyController extends Controller
{
	public function behaviors()
	{
		return [
			'verbs' => [
				'class' => VerbFilter::className(),
				'actions' => [
					'delete' => ['post'],
				],
			],
		];
	}

	/**
	 * Lists all LydbUser models.
	 * @return mixed
	 */
	public function actionIndex()
	{
		$goodId = Yii::$app->request->post('goodId');
		if($goodId){
			$goodsInfo = (new BuyService())->getGoodInfo($goodId);
			if($goodsInfo['status'] > 0){
				\Yii::$app->getSession()->setFlash('error', '已开奖，不能下单');
			}
			return $this->render('index',['info'=>$goodsInfo]);
		}else{
			return $this->render('index');
		}
	}

	public function actionHotGoods(){
		$models = (new BuyService())->getHotGoods();
		return $this->render('hotGoods',['models'=>$models]);
	}

	public function actionCreateBuyJob(){

		$goodId = Yii::$app->request->post('goodId');
		$buyNumber = (int) Yii::$app->request->post('buyNumber');

		if($goodId && $buyNumber > 0){
			$buyService = new BuyService();
			$buyJob = $buyService->createBuyJob($goodId,$buyNumber,10);
			
			if($buyJob){
				\Yii::$app->getSession()->setFlash('success', '创建任务成功');
				$this->redirect(['buy-job-user/index','BuyJobUserSearch[buyjob_id]'=>$buyJob->id]);
			}else{
				return $this->renderContent("创建失败");
			}
		}else{
			return $this->renderContent("参数错误");
		}
	}
	public function actionTest(){
		$user = (new LydbUserQuery())->findOne(['mobile'=>'18938728189']);

		$buyService = new BuyService();
		$a = $buyService->cleanCart($user);

		dump($a);
	}
}
